//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "SpreadedSmileSection.h"
using namespace Cephei::QL::Termstructures::Volatility;
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Termstructures/Volatility/SmileSection.h>
#include <gen/QL/Quote.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Volatility::CSpreadedSmileSection::CSpreadedSmileSection (Cephei::QL::Termstructures::Volatility::ISmileSection^ underlyingSection, Cephei::QL::IQuote^ spread) : CSmileSection(CSpreadedSmileSection::typeid)
{
    CSmileSection^ _CunderlyingSection;
    CQuote^ _Cspread;
    try
    {
#ifdef HANDLE
        _phSpreadedSmileSection = NULL;
#endif
        _CunderlyingSection = safe_cast<CSmileSection^> (underlyingSection);
        _CunderlyingSection->Lock();
        boost::shared_ptr<QuantLib::SmileSection>& _underlyingSection = static_cast<boost::shared_ptr<QuantLib::SmileSection>&> (_CunderlyingSection->GetShared ()); 
        _Cspread = safe_cast<CQuote^> (spread);
        _Cspread->Lock();
        Handle<QuantLib::Quote>& _spread = static_cast<Handle<QuantLib::Quote>&> (_Cspread->GetHandle ()); 
        _ppSpreadedSmileSection = new boost::shared_ptr<QuantLib::SpreadedSmileSection> (new QuantLib::SpreadedSmileSection ( _underlyingSection,  _spread ));
        SetSmileSection (boost::dynamic_pointer_cast<QuantLib::SmileSection> (*_ppSpreadedSmileSection));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CunderlyingSection != nullptr) _CunderlyingSection->Unlock();
        if (_Cspread != nullptr) _Cspread->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::CSpreadedSmileSection::CSpreadedSmileSection (boost::shared_ptr<QuantLib::SpreadedSmileSection>& childNative, Object^ owner) : CSmileSection(CSpreadedSmileSection::typeid)
{
#ifdef HANDLE
	_phSpreadedSmileSection = NULL;
#endif
	_ppSpreadedSmileSection = &childNative;
    _ppSmileSection = new boost::shared_ptr<QuantLib::SmileSection> (boost::dynamic_pointer_cast<QuantLib::SmileSection> (*_ppSpreadedSmileSection));
}
Cephei::QL::Termstructures::Volatility::CSpreadedSmileSection::CSpreadedSmileSection (QuantLib::SpreadedSmileSection& childNative, Object^ owner) : CSmileSection(CSpreadedSmileSection::typeid)
{
#ifdef HANDLE
	_phSpreadedSmileSection = NULL;
#endif
	_ppSpreadedSmileSection = new boost::shared_ptr<QuantLib::SpreadedSmileSection> (&childNative);
    _ppSmileSection = new boost::shared_ptr<QuantLib::SmileSection> (boost::dynamic_pointer_cast<QuantLib::SmileSection> (*_ppSpreadedSmileSection));
    _SpreadedSmileSectionOwner = owner;
    _SmileSectionOwner = owner;
}

Cephei::QL::Termstructures::Volatility::CSpreadedSmileSection::CSpreadedSmileSection (CSpreadedSmileSection^ copy) : CSmileSection(CSpreadedSmileSection::typeid)
{
#ifdef HANDLE
	_phSpreadedSmileSection = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSpreadedSmileSection = new boost::shared_ptr<QuantLib::SpreadedSmileSection> (copy->GetShared());
        _ppSmileSection = new boost::shared_ptr<QuantLib::SmileSection> (boost::dynamic_pointer_cast<QuantLib::SmileSection> (*_ppSpreadedSmileSection));
    }
}
Cephei::QL::Termstructures::Volatility::CSpreadedSmileSection::CSpreadedSmileSection (PLATFORM::Type^ t) : CSmileSection(CSpreadedSmileSection::typeid)
{
#ifdef HANDLE
	_phSpreadedSmileSection = NULL;
#endif
	if (!t->IsSubclassOf(CSpreadedSmileSection::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Volatility::CSpreadedSmileSection::CSpreadedSmileSection (QuantLib::Handle<QuantLib::SpreadedSmileSection>& childNative, Object^ owner)  : CSmileSection(CSpreadedSmileSection::typeid)
{
	_phSpreadedSmileSection = &childNative;
	_ppSpreadedSmileSection = &static_cast<boost::shared_ptr<QuantLib::SpreadedSmileSection>>(childNative.currentLink());
    _ppSmileSection = new boost::shared_ptr<QuantLib::SmileSection> (boost::dynamic_pointer_cast<QuantLib::SmileSection> (*_ppSpreadedSmileSection));
    _SpreadedSmileSectionOwner = owner;
}
Cephei::QL::Termstructures::Volatility::CSpreadedSmileSection::CSpreadedSmileSection (QuantLib::Handle<QuantLib::SpreadedSmileSection> childNative)  : CSmileSection(CSpreadedSmileSection::typeid)
{
	_phSpreadedSmileSection = &childNative;
	_ppSpreadedSmileSection = &static_cast<boost::shared_ptr<QuantLib::SpreadedSmileSection>>(childNative.currentLink());
    _ppSmileSection = new boost::shared_ptr<QuantLib::SmileSection> (boost::dynamic_pointer_cast<QuantLib::SmileSection> (*_ppSpreadedSmileSection));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Volatility::CSpreadedSmileSection::CSpreadedSmileSection (QuantLib::SpreadedSmileSection childNative)  : CSmileSection(CSpreadedSmileSection::typeid)
{
#ifdef HANDLE
	_phSpreadedSmileSection = NULL;
#endif
	_ppSpreadedSmileSection = new boost::shared_ptr<QuantLib::SpreadedSmileSection> (new QuantLib::SpreadedSmileSection (childNative));
    _ppSmileSection = new boost::shared_ptr<QuantLib::SmileSection> (boost::dynamic_pointer_cast<QuantLib::SmileSection> (*_ppSpreadedSmileSection));
}
#endif

Cephei::QL::Termstructures::Volatility::CSpreadedSmileSection::~CSpreadedSmileSection ()
{
    if (_ppSpreadedSmileSection != NULL)
    {
	    delete _ppSpreadedSmileSection;
        _ppSpreadedSmileSection = NULL;
    }
}
Cephei::QL::Termstructures::Volatility::CSpreadedSmileSection::!CSpreadedSmileSection ()
{
    if (_ppSpreadedSmileSection != NULL)
    {
	    delete _ppSpreadedSmileSection;
    }
}
QuantLib::SpreadedSmileSection& Cephei::QL::Termstructures::Volatility::CSpreadedSmileSection::GetReference ()
{
    if (_ppSpreadedSmileSection == NULL) throw REFNEW NativeNullException ();
	return **_ppSpreadedSmileSection;
}
boost::shared_ptr<QuantLib::SpreadedSmileSection>& Cephei::QL::Termstructures::Volatility::CSpreadedSmileSection::GetShared ()
{
    if (_ppSpreadedSmileSection == NULL) throw REFNEW NativeNullException ();
	return *_ppSpreadedSmileSection;
}
QuantLib::SpreadedSmileSection* Cephei::QL::Termstructures::Volatility::CSpreadedSmileSection::GetPointer ()
{
    if (_ppSpreadedSmileSection == NULL) throw REFNEW NativeNullException ();
	return &**_ppSpreadedSmileSection;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SpreadedSmileSection>& Cephei::QL::Termstructures::Volatility::CSpreadedSmileSection::GetHandle ()
{
	if (_phSpreadedSmileSection == NULL)
	{
		_phSpreadedSmileSection = new Handle<QuantLib::SpreadedSmileSection> (*_ppSpreadedSmileSection);
	}
	return *_phSpreadedSmileSection;
}
#endif
bool Cephei::QL::Termstructures::Volatility::CSpreadedSmileSection::HasNative () 
{
	return (_ppSpreadedSmileSection != NULL);
}

Double Cephei::QL::Termstructures::Volatility::CSpreadedSmileSection::AtmLevel::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSpreadedSmileSection)->atmLevel ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IDayCounter^ Cephei::QL::Termstructures::Volatility::CSpreadedSmileSection::DayCounter::get ()
{
    try
    {
    	QuantLib::DayCounter& _rv = (QuantLib::DayCounter&)(*_ppSpreadedSmileSection)->dayCounter ( );   
        Cephei::QL::Times::CDayCounter^ _nrv = REFNEW Cephei::QL::Times::CDayCounter (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Termstructures::Volatility::CSpreadedSmileSection::ExerciseDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppSpreadedSmileSection)->exerciseDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::CSpreadedSmileSection::ExerciseTime::get ()
{
    try
    {
    	QuantLib::Time _rv = (QuantLib::Time)(*_ppSpreadedSmileSection)->exerciseTime ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::CSpreadedSmileSection::MaxStrike::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSpreadedSmileSection)->maxStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::CSpreadedSmileSection::MinStrike::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSpreadedSmileSection)->minStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::Volatility::ISpreadedSmileSection^ Cephei::QL::Termstructures::Volatility::CSpreadedSmileSection::Update::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppSpreadedSmileSection)->update ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Volatility::ISpreadedSmileSection^ Cephei::QL::Termstructures::Volatility::CSpreadedSmileSection_Factory::Create (Cephei::QL::Termstructures::Volatility::ISmileSection^ underlyingSection, Cephei::QL::IQuote^ spread)
{
    return REFNEW CSpreadedSmileSection ( underlyingSection,  spread);
}
